<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class attend_service{
	private $CI;
	function __construct() { 
		$this->CI = &get_instance();
		$this->CI->load->model('Attend_model');
		$this->CI->load->model('Attend_student_status_model');
		$this->CI->load->model('Attend_students_norm_model');
		$this->CI->load->model('Attend_teaching_content_model');
		$this->CI->load->model('Attend_teaching_effect_model');
		$this->CI->load->model('Attend_teaching_implementation_model');
		$this->CI->load->model('Attend_teaching_level_model');
		$this->CI->load->model('Attend_teaching_readiness_model');
		$this->CI->load->model('Attend_classroom_facilities_model');
        $this->CI->load->model('Attend_classroom_environmental_model');
		$this->CI->load->model('Attend_record_model');
		$this->CI->load->model('Attend_img_model');
		$this->CI->load->model('Class_model');
		$this->CI->load->model('Admin_model');
	}

	/**  
	* 获取听课表基础数据（仅主表）
	* @access public 
	* @param  $attend_id 听课表id
	* @return array or bool
	*/  
	public function getAttendById($attend_id) {
		$attend=$this->CI->Attend_model->get_single(array('id'=>$attend_id));
		if($attend&&count($attend)>0){
			return $attend;
		}
		else{
			return false;
		}
	}
	
	/**  
	* 获取听课表页面数据
	* @access public 
	* @param  $attend_id 听课表id 当为新增时传递0
	* @param  $class_id 课程id 新增时传递 default false
	* @param  $listen_id 听课人id 新增时传递 default false
	* @return array
	*/  
	public function getAttendData($attend_id,$class_id=false,$listen_id=false) {
		//主表数据
		$attend=$this->getAttendById($attend_id);
		//两种情况，1：有数据  2：没数据
		if($attend){
			// 教学内容得分表（attend_teaching_content）
			$attend_teaching_content=$this->CI->Attend_teaching_content_model->get_list(array('attend_id'=>$attend_id));
			// 教学实施得分表（attend_teaching_implementation）
			$attend_teaching_implementation=$this->CI->Attend_teaching_implementation_model->get_list(array('attend_id'=>$attend_id));
			// 教学水平总体评价表（attend_teaching_level）
			$attend_teaching_level=$this->CI->Attend_teaching_level_model->get_list(array('attend_id'=>$attend_id));
			// 学生状况得分表（attend_student_status）
			$attend_student_status=$this->CI->Attend_student_status_model->get_list(array('attend_id'=>$attend_id));
			// 教学效果总体评价表（attend_teaching_effect）
			$attend_teaching_effect=$this->CI->Attend_teaching_effect_model->get_list(array('attend_id'=>$attend_id));
			// 教学准备开展情况表（attend_teaching_readiness）
			$attend_teaching_readiness=$this->CI->Attend_teaching_readiness_model->get_list(array('attend_id'=>$attend_id));
			// 学生常态表（attend_students_norm）
			$attend_students_norm=$this->CI->Attend_students_norm_model->get_list(array('attend_id'=>$attend_id));
			// 课堂设施设备完好情况（attend_classroom_facilities）
			$attend_classroom_facilities=$this->CI->Attend_classroom_facilities_model->get_single(array('attend_id'=>$attend_id));
			// 课堂环境卫生情况表（attend_classroom_environmental）
			$attend_classroom_environmental=$this->CI->Attend_classroom_environmental_model->get_single(array('attend_id'=>$attend_id));
			// 听课内容记录表（attend_record）
			$attend_record=$this->CI->Attend_record_model->get_list(array('attend_id'=>$attend_id));
			// 听课图片表（attend_img）
			$attend_img=$this->CI->Attend_img_model->get_list(array('attend_id'=>$attend_id));
		}
		else{
			//主表数据
			$connar['cond']=array('class.id'=>$class_id);
			$connar['join']=array('admin a','a.id=class.teacher_id');
			$connar['items']=" class.*,a.username,a.title,a.dept_name,a.age_groups,a.teachers_attribute ";
			$class=$this->CI->Class_model->get_single_full($connar);
			$listen=$this->CI->Admin_model->get_single(array('id'=>$listen_id));
			$attend=array(
				'class_id'=>$class_id,
				'listen_id'=>$listen_id,
				'serial_no'=>buildOrdeNo(),
				'academic_year'=>'',
				'semester'=>'',
				'class_name'=>isset($class['name'])?$class['name']:'',
				'class_type'=>isset($class['type'])?$class['type']:'',
				'teacher_name'=>isset($class['username'])?$class['username']:'',
				'teacher_code'=>'',
				'title'=>isset($class['title'])?$class['title']:'',
				'dept_name'=>isset($class['dept_name'])?$class['dept_name']:'',
				'age_groups'=>isset($class['age_groups'])?$class['age_groups']:'',
				'teachers_attribute'=>isset($class['teachers_attribute'])?$class['teachers_attribute']:'',
				'listen_name'=>$listen['username'],
				'listen_time'=>time(),
				'listen_address'=>'',
				'course_units'=>isset($class['course_units'])?$class['course_units']:'',
				'listen_class'=>isset($class['classroom'])?$class['classroom']:'',
				// 'classroom_facilities'=>'',
				// 'classroom_environmental'=>'',
				'advice'=>'',
				'teaching_level_score'=>'',
				'teaching_effect_score'=>''
				);
			// 教学内容得分表（attend_teaching_content）
			$_attend_teaching_content=(new TeachingContent)->getAll();
			$attend_teaching_content=array();
			foreach ($_attend_teaching_content as $k => $v) {
				$attend_teaching_content[$k]=array(
					'list_id'=>$v[XPHEnum::Alternate],
					'key_index'=>$v[XPHEnum::Value],
					'content'=>$v[XPHEnum::Caption],
					'score'=>$v[XPHEnum::Remark],
					'get_score'=>''
					);
			}
			// 教学实施得分表（attend_teaching_implementation）
			$_attend_teaching_implementation=(new TeachingImplementation)->getAll();
			$attend_teaching_implementation=array();
			foreach ($_attend_teaching_implementation as $k => $v) {
				$attend_teaching_implementation[$k]=array(					
					'list_id'=>$v[XPHEnum::Alternate],
					'key_index'=>$v[XPHEnum::Value],
					'content'=>$v[XPHEnum::Caption],
					'score'=>$v[XPHEnum::Remark],
					'get_score'=>''
					);
			}
			// 教学水平总体评价表（attend_teaching_level）
			$_attend_teaching_level=(new TeachingLevel)->getAll();
			$attend_teaching_level=array();
			foreach ($_attend_teaching_level as $k => $v) {
				$attend_teaching_level[$k]=array(
					'item'=>$v[XPHEnum::Caption],
					'remark'=>$v[XPHEnum::Remark],
					'is_check'=>CommonWhether::Not[XPHEnum::Value]
					);
			}
			// 学生状况得分表（attend_student_status）
			$_attend_student_status=(new StudentStatus)->getAll();
			$attend_student_status=array();
			foreach ($_attend_student_status as $k => $v) {
				$attend_student_status[$k]=array(
					'list_id'=>$v[XPHEnum::Alternate],
					'key_index'=>$v[XPHEnum::Value],
					'content'=>$v[XPHEnum::Caption],
					'score'=>$v[XPHEnum::Remark],
					'get_score'=>''
					);
			}
			// 教学效果总体评价表（attend_teaching_effect）
			$_attend_teaching_effect=(new TeachingEffect)->getAll();
			$attend_teaching_effect=array();
			foreach ($_attend_teaching_effect as $k => $v) {
				$attend_teaching_effect[$k]=array(
					'item'=>$v[XPHEnum::Caption],
					'remark'=>$v[XPHEnum::Remark],
					'is_check'=>CommonWhether::Not[XPHEnum::Value]
					);
			}
			// 教学准备开展情况表（attend_teaching_readiness）
			$_attend_teaching_readiness=(new TeachingReadiness)->getAll();
			$attend_teaching_readiness=array();
			foreach ($_attend_teaching_readiness as $k => $v) {
				$attend_teaching_readiness[$k]=array(
					'item'=>$v[XPHEnum::Caption],
					'option'=>implode(',',$v[XPHEnum::Remark]),
					'check_value'=>''
					);
			}
			// 学生常态表（attend_students_norm）
			$_attend_students_norm=(new StudentsNorm)->getAll();
			$attend_students_norm=array();
			foreach ($_attend_students_norm as $k => $v) {
				$attend_students_norm[$k]=array(
					'item'=>$v[XPHEnum::Caption],
					'check_value'=>''
					);
			}
			// 课堂设施设备完好情况（attend_classroom_facilities）
			$_attend_classroom_facilities=(new ClassroomFacilities)->getAll();
			$attend_classroom_facilities_option=array_column($_attend_classroom_facilities, XPHEnum::Caption);
			$attend_classroom_facilities=array('option'=>implode(',',$attend_classroom_facilities_option),'check_value'=>'');
			// 课堂环境卫生情况表（attend_classroom_environmental）
			$_attend_classroom_environmental=(new ClassroomEnvironmental)->getAll();
			$attend_classroom_environmental_option=array_column($_attend_classroom_environmental, XPHEnum::Caption);
			$attend_classroom_environmental=array('option'=>implode(',',$attend_classroom_environmental_option),'check_value'=>'');
			// 听课内容记录表（attend_record）
			$attend_record=array();
			// 听课图片表（attend_img）
			$attend_img=array();
		}
		
		$result=array(
			'attend'=>$attend,
			'attend_teaching_content'=>$attend_teaching_content,
			'attend_teaching_implementation'=>$attend_teaching_implementation,
			'attend_teaching_level'=>$attend_teaching_level,
			'attend_student_status'=>$attend_student_status,
			'attend_teaching_effect'=>$attend_teaching_effect,
			'attend_teaching_readiness'=>$attend_teaching_readiness,
			'attend_students_norm'=>$attend_students_norm,
			'attend_classroom_facilities'=>$attend_classroom_facilities,
			'attend_classroom_environmental'=>$attend_classroom_environmental,
			'attend_record'=>$attend_record,
			'attend_img'=>$attend_img
			);
		return $result;
	}


	/**  
	* 听课表数据保存
	* @access public 
	* @param  $param 表单数据
	* @param  $attend_id 听课表id 新增为0 default 0
	* @return int
	*/  
	public function saveAttend($param,$attend_id=0) {
		//主表数据
		$attend_default=$this->getAttendById($attend_id);

		//主表操作
		$attend_param=$param['attend'];
		$attend=array(
			'class_id'=>$attend_param['class_id'],
			'listen_id'=>$attend_param['listen_id'],
			'type'=>$attend_param['type'],
			// 'serial_no'=>$attend_param['serial_no'],
			'academic_year'=>$attend_param['academic_year'],
			'semester'=>$attend_param['semester'],
			'class_name'=>$attend_param['class_name'],
			'class_type'=>$attend_param['class_type'],
			'teacher_name'=>$attend_param['teacher_name'],
			'teacher_code'=>$attend_param['teacher_code'],
			'title'=>$attend_param['title'],
			'dept_name'=>$attend_param['dept_name'],
			'age_groups'=>$attend_param['age_groups'],
			'teachers_attribute'=>$attend_param['teachers_attribute'],
			'listen_name'=>$attend_param['listen_name'],
			'listen_time'=>strtotime($attend_param['listen_time']),
			'listen_address'=>$attend_param['listen_address'],
			'course_units'=>$attend_param['course_units'],
			'listen_class'=>$attend_param['listen_class'],
			// 'classroom_facilities'=>$attend_param['classroom_facilities'],
			// 'classroom_environmental'=>$attend_param['classroom_environmental'],
			'advice'=>$attend_param['advice'],
			'teaching_level_score'=>$attend_param['teaching_level_score'],
			'teaching_effect_score'=>$attend_param['teaching_effect_score'],
			'status'=>$attend_param['status'],
			// 0415新增总分 教学70%+学生30%
			'score'=>$attend_param['score'],
			// 0723新增听课类型
			'listen_type'=>$attend_param['listen_type']
		);
		//新增，添加created
		if(!$attend_default){
			$attend['created']=time();
			$attend_id=$this->CI->Attend_model->add($attend);
		}
		//修改，添加id和updated
		else{
			$attend['id']=$attend_id;
			$attend['updated']=time();
			$this->CI->Attend_model->update($attend);
		}

		//子表
		//数据清除
		$this->CI->Attend_teaching_content_model->delBatch($this->CI->sqlEscape(" attend_id=?? ",array($attend_id)));
		$this->CI->Attend_teaching_implementation_model->delBatch($this->CI->sqlEscape(" attend_id=?? ",array($attend_id)));
		$this->CI->Attend_teaching_level_model->delBatch($this->CI->sqlEscape(" attend_id=?? ",array($attend_id)));
		$this->CI->Attend_student_status_model->delBatch($this->CI->sqlEscape(" attend_id=?? ",array($attend_id)));
		$this->CI->Attend_teaching_effect_model->delBatch($this->CI->sqlEscape(" attend_id=?? ",array($attend_id)));
		$this->CI->Attend_teaching_readiness_model->delBatch($this->CI->sqlEscape(" attend_id=?? ",array($attend_id)));
		$this->CI->Attend_students_norm_model->delBatch($this->CI->sqlEscape(" attend_id=?? ",array($attend_id)));
		$this->CI->Attend_classroom_facilities_model->delBatch($this->CI->sqlEscape(" attend_id=?? ",array($attend_id)));
		$this->CI->Attend_classroom_environmental_model->delBatch($this->CI->sqlEscape(" attend_id=?? ",array($attend_id)));
		$this->CI->Attend_record_model->delBatch($this->CI->sqlEscape(" attend_id=?? ",array($attend_id)));
		$this->CI->Attend_img_model->delBatch($this->CI->sqlEscape(" attend_id=?? ",array($attend_id)));
		//定值
		$attend_teaching_content=$param['attend_teaching_content'];
		$attend_teaching_implementation=$param['attend_teaching_implementation'];
		$attend_teaching_level=$param['attend_teaching_level'];
		$attend_student_status=$param['attend_student_status'];
		$attend_teaching_effect=$param['attend_teaching_effect'];
		$attend_teaching_readiness=$param['attend_teaching_readiness'];
		$attend_students_norm=$param['attend_students_norm'];
		$attend_classroom_facilities=$param['attend_classroom_facilities'];
		$attend_classroom_environmental=$param['attend_classroom_environmental'];
		$attend_record=$param['attend_record'];
		$attend_img=$param['attend_img'];

		// 教学内容得分表（attend_teaching_content）
		$default_attend_teaching_content_sql=$attend_teaching_content_sql=" insert into db_attend_teaching_content (attend_id,content,score,get_score,key_index,type,list_id) values ";
		foreach ($attend_teaching_content as $k => $v) {
			if($default_attend_teaching_content_sql!=$attend_teaching_content_sql){
				$attend_teaching_content_sql.=" , ";
			}
			$attend_teaching_content_sql.=$this->CI->sqlEscape(" (??,??,??,??,??,??,??) ",array($attend_id,$v['content'],$v['score'],$v['get_score'],$v['key_index'],TeachingType::Content[XPHEnum::Value],$v['list_id']));
		}
		$this->CI->Attend_teaching_content_model->query($attend_teaching_content_sql,false);

		// 教学实施得分表（attend_teaching_implementation）
		$default_attend_teaching_implementation_sql=$attend_teaching_implementation_sql=" insert into db_attend_teaching_implementation (attend_id,content,score,get_score,key_index,type,list_id) values ";
		foreach ($attend_teaching_implementation as $k => $v) {
			if($default_attend_teaching_implementation_sql!=$attend_teaching_implementation_sql){
				$attend_teaching_implementation_sql.=" , ";
			}
			$attend_teaching_implementation_sql.=$this->CI->sqlEscape(" (??,??,??,??,??,??,??) ",array($attend_id,$v['content'],$v['score'],$v['get_score'],$v['key_index'],TeachingType::Implementation[XPHEnum::Value],$v['list_id']));
		}
		$this->CI->Attend_teaching_implementation_model->query($attend_teaching_implementation_sql,false);

		// 教学水平总体评价表（attend_teaching_level）
		$default_attend_teaching_level_sql=$attend_teaching_level_sql=" insert into db_attend_teaching_level (attend_id,item,remark,is_check) values ";
		foreach ($attend_teaching_level as $k => $v) {
			if($default_attend_teaching_level_sql!=$attend_teaching_level_sql){
				$attend_teaching_level_sql.=" , ";
			}
			$attend_teaching_level_sql.=$this->CI->sqlEscape(" (??,??,??,??) ",array($attend_id,$v['item'],$v['remark'],$v['is_check']));
		}
		$this->CI->Attend_teaching_level_model->query($attend_teaching_level_sql,false);

		// 学生状况得分表（attend_student_status）
		$default_attend_student_status_sql=$attend_student_status_sql=" insert into db_attend_student_status (attend_id,content,score,get_score,key_index,type,list_id) values ";
		foreach ($attend_student_status as $k => $v) {
			if($default_attend_student_status_sql!=$attend_student_status_sql){
				$attend_student_status_sql.=" , ";
			}
			$attend_student_status_sql.=$this->CI->sqlEscape(" (??,??,??,??,??,??,??) ",array($attend_id,$v['content'],$v['score'],$v['get_score'],$v['key_index'],TeachingType::StudentStatus[XPHEnum::Value],$v['list_id']));
		}
		$this->CI->Attend_student_status_model->query($attend_student_status_sql,false);

		// 教学效果总体评价表（attend_teaching_effect）
		$default_attend_teaching_effect_sql=$attend_teaching_effect_sql=" insert into db_attend_teaching_effect (attend_id,item,remark,is_check) values ";
		foreach ($attend_teaching_effect as $k => $v) {
			if($default_attend_teaching_effect_sql!=$attend_teaching_effect_sql){
				$attend_teaching_effect_sql.=" , ";
			}
			$attend_teaching_effect_sql.=$this->CI->sqlEscape(" (??,??,??,??) ",array($attend_id,$v['item'],$v['remark'],$v['is_check']));
		}
		$this->CI->Attend_teaching_effect_model->query($attend_teaching_effect_sql,false);

		// 教学准备开展情况表（attend_teaching_readiness）
		$default_attend_teaching_readiness_sql=$attend_teaching_readiness_sql=" insert into db_attend_teaching_readiness (attend_id,item,option,check_value) values ";
		foreach ($attend_teaching_readiness as $k => $v) {
			if($default_attend_teaching_readiness_sql!=$attend_teaching_readiness_sql){
				$attend_teaching_readiness_sql.=" , ";
			}
			$attend_teaching_readiness_sql.=$this->CI->sqlEscape(" (??,??,??,??) ",array($attend_id,$v['item'],$v['option'],$v['check_value']));
		}
		$this->CI->Attend_teaching_readiness_model->query($attend_teaching_readiness_sql,false);

		// 学生常态表（attend_students_norm）
		$default_attend_students_norm_sql=$attend_students_norm_sql=" insert into db_attend_students_norm (attend_id,item,check_value) values ";
		foreach ($attend_students_norm as $k => $v) {
			if($default_attend_students_norm_sql!=$attend_students_norm_sql){
				$attend_students_norm_sql.=" , ";
			}
			$attend_students_norm_sql.=$this->CI->sqlEscape(" (??,??,??) ",array($attend_id,$v['item'],$v['check_value']));
		}
		$this->CI->Attend_students_norm_model->query($attend_students_norm_sql,false);

		// 课堂设施设备完好情况表（attend_classroom_facilities）
		$this->CI->Attend_classroom_facilities_model->add(array('attend_id'=>$attend_id,'option'=>$attend_classroom_facilities['option'],'check_value'=>$attend_classroom_facilities['check_value']));

		// 课堂环境卫生情况表（attend_classroom_environmental）
		$this->CI->Attend_classroom_environmental_model->add(array('attend_id'=>$attend_id,'option'=>$attend_classroom_environmental['option'],'check_value'=>$attend_classroom_environmental['check_value']));

		// 听课内容记录表（attend_record）
		if(count($attend_record)>0){
			$default_attend_record_sql=$attend_record_sql=" insert into db_attend_record (attend_id,time,commentary) values ";
			foreach ($attend_record as $k => $v) {
				if($default_attend_record_sql!=$attend_record_sql){
					$attend_record_sql.=" , ";
				}
				$attend_record_sql.=$this->CI->sqlEscape(" (??,??,??) ",array($attend_id,$v['time'],$v['commentary']));
			}
			$this->CI->Attend_record_model->query($attend_record_sql,false);
		}

		// 听课图片表（attend_img）
		if(count($attend_img)>0){
			$default_attend_img_sql=$attend_img_sql=" insert into db_attend_img (attend_id,img) values ";
			foreach ($attend_img as $k => $v) {
				if($default_attend_img_sql!=$attend_img_sql){
					$attend_img_sql.=" , ";
				}
				$attend_img_sql.=$this->CI->sqlEscape(" (??,??) ",array($attend_id,$v['img']));
			}
			$this->CI->Attend_img_model->query($attend_img_sql,false);
		}

		return $attend_id;
	}
	

	
	
}